<?php

namespace App\Http\Controllers\Wechat;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Wechat\WechatMenuRequest;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use App\Repositories\BLL\WechatMenuBLL;
use EasyWeChat\Foundation\Application;

class WechatMenuController extends Controller
{
	
	private $menuBLL,$menuService;
	private $currentRouteName;
	
	public function __construct(WechatMenuBLL $menu,Application $wechat){
		$this->currentRouteName = Route::currentRouteName();
		$this->menuBLL = $menu;
		$this->menuService = $wechat->menu;
	}	
	
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $menus = $this->menuBLL->getAll($this->currentRouteName);

		$addButton = sp_get_AddActionButton($this->currentRouteName);
		$submitButton = sp_get_AddActionButton($this->currentRouteName,"发布","addMenu");
		$delButton = sp_get_AddActionButton($this->currentRouteName,"删除菜单","delMenu");
		
		return view('wechat.menu.index',
        					["menus"=>$menus
        					,"addButton"=>$addButton
        					,"submitButton"=>$submitButton
        					,"delButton"=>$delButton]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
    		$parents = $this->getLevel0();
        return view('wechat.menu.create',
					['parents'=>$parents]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(WechatMenuRequest $request)
    {
        $result = $this->menuBLL->createEntity($request->all());
		if($result){
			flash("创建成功");
		}else{
			flash("创建失败","error");
		}
        return redirect(route('wechatmenu.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $menu = $this->menuBLL->editView($id);
        return view('wechat.menu.show',
						["menu"=>$menu]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $menu = $this->menuBLL->editView($id);
		$parents = $this->getLevel0();
        return view('wechat.menu.edit',
						["menu"=>$menu
						,'parents'=>$parents]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(WechatMenuRequest $request, $id)
    {
        $result = $this->menuBLL->updateEntity($request->all(),$id);
		if($result){
			flash("更新成功");
		}else{
			flash("更新失败","error");
		}
        return redirect(route('wechatmenu.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->menuBLL->destroyEntity($id);
        return redirect(route('wechatmenu.index'));
    }
	
	public function addMenu(){
		//获取menu格式
		$menus = $this->menuBLL->getAll($this->currentRouteName);
		$buttons = [];
		foreach($menus as $menu){
			$b1=array();
			$b1['name'] = $menu->name;
			if(count($menu->child)===0){
				$b1['type'] = 'view';
				$b1['url'] = $menu->url;
			}else{
				$childMenu = [];
				foreach($menu->child as $c){
					$b2=array();
					$b2['type'] = 'view';
					$b2['name'] = $c->name;
					$b2['url'] = $c->url;
					$childMenu[] = $b2;
				}
				$b1['sub_button'] = $childMenu;
			}
			$buttons[]=$b1;
		}
		$this->menuService->add($buttons);
		flash("发布成功");
		return redirect(route('wechatmenu.index'));
	}
	
	public function delMenu(){
		$this->menuService->destroy();
		flash("删除成功");
		return redirect(route('wechatmenu.index'));
	}
	
	private function getLevel0(){
		$parent = DB::table('wechat_menus')
					->where('pid','=',0)
					->select('id','name')
					->orderby('sort')
					->get();
		return $parent;
	}
}
